package com.qmerak.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.qmerak.common.BSCalendar;
import com.qmerak.common.SysConstants;
import com.qmerak.entity.ChangeUnionEntity;
import com.qmerak.entity.LogoffEntity;
import com.qmerak.entity.MemberEntity;
import com.qmerak.entity.OrgEntity;
import com.qmerak.service.IChangeUnionService;
import com.qmerak.service.ILogoffService;
import com.qmerak.service.IMemberService;
import com.qmerak.service.IOrgService;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-4-14
 * @project WebTest
 */
@Controller
@RequestMapping("/member")
@SessionAttributes({ "cur_user" })
public class MemberAction {
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IOrgService orgService;
	@Autowired
	private ILogoffService logoffService;
	@Autowired
	private IChangeUnionService changeUnionService;

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(MemberEntity user, ModelMap modelMap) {
		if (memberService.verify(user, modelMap)) {
			return SysConstants.REDIRECT_PREFIX + SysConstants.SYS_MAIN_PAGE;
		} else {
			return SysConstants.SYS_LOGIN_PAGE
					+ "?msga=账号或密码不正确，请重新确认后输入！&amp;loginName="
					+ user.getLoginName() + "&amp;loginPWD="
					+ user.getLoginPWD();
		}
	}

	@ResponseBody
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void add(MemberEntity entity) {
		memberService.add(entity);
	}

	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public void update(MemberEntity entity) {
		memberService.update(entity);
	}

	@ResponseBody
	@RequestMapping(value = "roles/update", method = RequestMethod.POST)
	public void updateRoles(MemberEntity entity) {
		MemberEntity oldEntity = memberService.find(entity);
		oldEntity.setRoles(entity.getRoles());
		memberService.update(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "statu/update", method = RequestMethod.POST)
	public void updateState(MemberEntity memberEntity, ModelMap modelMap) {
		MemberEntity loginMemberEntity = (MemberEntity) modelMap
				.get(SysConstants.SYS_LOGIN_USER);
		memberService.updateStatus(memberEntity, loginMemberEntity);
	}

	@ResponseBody
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public void delete(MemberEntity entity) {
		memberService.delete(entity);
	}

	@ResponseBody
	@RequestMapping(value = "logoff", method = RequestMethod.POST)
	public void logOff(LogoffEntity entity) {
		entity.setApplyDate(BSCalendar.now().toString("yyyy-mm-dd"));
		logoffService.add(entity);
	}

	@ResponseBody
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	public void logOut(MemberEntity entity) {
		MemberEntity memberEntity = memberService.find(entity.getId());
		memberEntity.setStatus(MemberEntity.Member_Status.LOGOFFED.name());
		memberService.update(memberEntity);
	}

	@ResponseBody
	@RequestMapping(value = "changeunion", method = RequestMethod.POST)
	public void changeUnion(ChangeUnionEntity entity) {
		entity.setApplyDate(BSCalendar.now().toString("yyyy-mm-dd"));
		changeUnionService.add(entity);
	}

	@ResponseBody
	@RequestMapping(value = "all", method = RequestMethod.GET)
	public List<MemberEntity> findAll() {
		return memberService.list();
	}

	@ResponseBody
	@RequestMapping(value = "logined", method = RequestMethod.GET)
	public List<MemberEntity> findMember() {
		return memberService.findMembers();
	}

	@ResponseBody
	@RequestMapping(value = "verifyIDNumber", method = RequestMethod.GET)
	public boolean verifyID(MemberEntity memberEntity) {
		return memberService.verifyIDNumber(memberEntity);
	}

	@ResponseBody
	@RequestMapping(value = "batch/update", method = RequestMethod.POST)
	public void batchUpdate(MemberEntity memberEntity) {
		MemberEntity oldEntity = memberService.find(memberEntity);
		oldEntity.setIdNumber(memberEntity.getIdNumber());
		oldEntity.setName(memberEntity.getName());
		memberService.update(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "logining", method = RequestMethod.POST)
	public void loginingMember(MemberEntity memberEntity, ModelMap modelMap) {
		MemberEntity oldEntity = memberService.find(memberEntity);
		memberEntity.setIdNumber(oldEntity.getIdNumber());
		memberEntity.setName(oldEntity.getName());
		memberService.update(memberEntity);
		modelMap.addAttribute(SysConstants.SYS_LOGIN_USER, memberEntity);

	}

	@ResponseBody
	@RequestMapping(value = "modifypwd", method = RequestMethod.POST)
	public void modifyPWD(HttpServletRequest request) {
		MemberEntity loginUser = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		String newpwd = request.getParameter("newpwd");
		MemberEntity oldEntity = memberService.find(loginUser.getId());
		oldEntity.setLoginPWD(newpwd);
		memberService.updateAll(oldEntity);
	}

	@ResponseBody
	@RequestMapping(value = "resetpwd", method = RequestMethod.POST)
	public void resetPWD(MemberEntity memberEntity) {
		MemberEntity oldMember = memberService.find(memberEntity.getId());
		memberService.initPWD(oldMember);
		memberService.updateAll(oldMember);
	}

	@ResponseBody
	@RequestMapping(value = "logining", method = RequestMethod.GET)
	public List<MemberEntity> findLoginingMember(HttpServletRequest request) {
		MemberEntity memberEntity = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		List<OrgEntity> list = new ArrayList<OrgEntity>();
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(memberEntity
				.getLoginName())) {
			list = orgService.list();
		} else {
			String roleString = memberEntity.getRoles();
			if (null != roleString) {
				String[] strings = roleString.split(",");
				for (String string : strings) {
					if ("2".equals(string) || "3".equals(string)) {
						list.add(orgService.orgOfManager(memberEntity));
					}
				}
			}
		}
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (OrgEntity orgEntity : list) {
			result.addAll(memberService.loginingMemberOf(orgEntity));
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "logoffing", method = RequestMethod.GET)
	public List<MemberEntity> findLogoffingMember(HttpServletRequest request) {
		MemberEntity memberEntity = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		List<OrgEntity> list = new ArrayList<OrgEntity>();
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(memberEntity
				.getLoginName())) {
			list = orgService.list();
		} else {
			String roleString = memberEntity.getRoles();
			if (null != roleString) {
				String[] strings = roleString.split(",");
				for (String string : strings) {
					if ("2".equals(string) || "3".equals(string)) {
						list.add(orgService.orgOfManager(memberEntity));
					}
				}
			}
		}
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (OrgEntity orgEntity : list) {
			result.addAll(memberService.logoffingMemberOf(orgEntity));
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "unionchanging", method = RequestMethod.GET)
	public List<MemberEntity> findUionChangingMember(HttpServletRequest request) {
		MemberEntity memberEntity = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		List<OrgEntity> list = new ArrayList<OrgEntity>();
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(memberEntity
				.getLoginName())) {
			list = orgService.list();
		} else {
			String roleString = memberEntity.getRoles();
			if (null != roleString) {
				String[] strings = roleString.split(",");
				for (String string : strings) {
					if ("2".equals(string) || "3".equals(string)) {
						list.add(orgService.orgOfManager(memberEntity));
					}
				}
			}
		}
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (OrgEntity orgEntity : list) {
			result.addAll(memberService.unionChangingMemberOf(orgEntity));
		}
		return result;
	}

}
